import { put, takeEvery, all, call } from 'redux-saga/effects'; // tabeEvery 捕捉每一个action , put发送action到store里面

import axios from '../axios/axios'

// 请求mock数据
const apiFn = () => axios.get('/mock');

// 异步
function* getUser(action){
    // 参数action就是dispatch时方法中的参数

    let res = yield call(apiFn);
    console.log(res, '++++++++++我是saga请求回来的数据+++++++++++++')

    // 更新userInfo
    let action1 = {
        type:'SET_USER_INFO',
        payload: action.payload
    }

    // 更新userList
    let action2 = {
        type:'SET_USER_LIST',
        payload: res.data
    }

    // put提交这个actions 到store里面，和在reducer里面提交是一样的
    yield put(action1)
    yield put(action2)
}

// 监听异步
function* watchGetUser(){
    // takeEvery 捕获 type为GET_USER 的action
    yield takeEvery('GET_USER_LIST', getUser)
}

// 启动监听
export default function* rootSaga() {
    yield all([
        watchGetUser()
    ])
}
